package com.emenu.common;

import java.net.SocketTimeoutException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class PostWebServiceData {
	private static final String NameSpace = "http://tempuri.org/";
	private static final String MethodHitMasterInsert = "HitMaster_Insert";
	private static final String MethodHitPrint = "_PrintHitCode";
	
	public PostWebServiceData() {

	}
	
	/**确认下单
	 * 
	 * @param url
	 * @param bhm
	 * @param bhd
	 * @param bhs
	 * @return
	 */
	public static String PostHitMaster(String url, String bhm,String bhd,String bhs) {

		// Configuration cf = new Configuration();
		// url = cf.getValue("url");
		String soapAction = NameSpace + MethodHitMasterInsert;
		String result = "";

		try {

			// 指定WebService的命名空间和调用的方法名

			SoapObject request = new SoapObject(NameSpace, MethodHitMasterInsert);
			// step2设置调用方法的参数值

			request.addProperty("hit_master", bhm);
			request.addProperty("hit_detail", bhd);
			request.addProperty("hit_series", bhs);

			// 生成调用WebService方法的SOAP请求信息。
			// 该信息由SoapSerializationEnvelope对象描述

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

			SoapEnvelope.VER11);
			// 第1步创建的SoapObject对象。
			envelope.bodyOut = request;

			// 设置webservice是.Net
			envelope.dotNet = true;

			// 等于envelope.bodyOut = request;

			// envelope.setOutputSoapObject(request);

			// step4 创建HttpTransportSE对象,指定url

			HttpTransportSE ht = new HttpTransportSE(url);

			// call方法调用WebService方法

			// ht.call(soapAction, envelope);

			// //防止连接时间过期////////////////////////////

			try {
				Thread.sleep(300);
				ht.call(soapAction, envelope);

			} catch (SocketTimeoutException e1) {
				// TODO Auto-generated catch block
				try {
					ht.call(soapAction, envelope);
				} catch (SocketTimeoutException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			// //////////////////////////////

			// 若返回值有多个，则用SoapObject取，用getProperty来获得每个返回值
			// SoapObject soapObject = (SoapObject) envelope.getResponse();
			// SoapPrimitive value = (SoapPrimitive)soapObject.getProperty(0);

			// 若返回值是单个值，则只能用SoapPrimitive取，此时若用SoapObject强制转换会出异常
			if (envelope.getResponse() != null) {

				SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

				result = response.toString();
			}

		} catch (Exception e) {

			result = e.getMessage();

		}

		return result;

	}
	
	public static String PostHitPrint(String url,String code, String print_code,String username) {

		// Configuration cf = new Configuration();
		// url = cf.getValue("url");
		String soapAction = NameSpace + MethodHitPrint;
		String result = "";

		try {

			// 指定WebService的命名空间和调用的方法名

			SoapObject request = new SoapObject(NameSpace, MethodHitPrint);
			// step2设置调用方法的参数值

			request.addProperty("code", code);
			request.addProperty("print_code", print_code);
			request.addProperty("username", username);

			// 生成调用WebService方法的SOAP请求信息。
			// 该信息由SoapSerializationEnvelope对象描述

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

			SoapEnvelope.VER11);
			// 第1步创建的SoapObject对象。
			envelope.bodyOut = request;

			// 设置webservice是.Net
			envelope.dotNet = true;

			// 等于envelope.bodyOut = request;

			// envelope.setOutputSoapObject(request);

			// step4 创建HttpTransportSE对象,指定url

			HttpTransportSE ht = new HttpTransportSE(url);

			// call方法调用WebService方法

			// ht.call(soapAction, envelope);

			// //防止连接时间过期////////////////////////////

			try {
				Thread.sleep(300);
				ht.call(soapAction, envelope);

			} catch (SocketTimeoutException e1) {
				// TODO Auto-generated catch block
				try {
					ht.call(soapAction, envelope);
				} catch (SocketTimeoutException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			// //////////////////////////////

			// 若返回值有多个，则用SoapObject取，用getProperty来获得每个返回值
			// SoapObject soapObject = (SoapObject) envelope.getResponse();
			// SoapPrimitive value = (SoapPrimitive)soapObject.getProperty(0);

			// 若返回值是单个值，则只能用SoapPrimitive取，此时若用SoapObject强制转换会出异常
			if (envelope.getResponse() != null) {

				SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

				result = response.toString();
			}

		} catch (Exception e) {

			result = e.getMessage();

		}

		return result;

	}
}
